//
//  LYFDiscoverPageViewController.m
//  段子手
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "LYFDiscoverPageViewController.h"

@interface LYFDiscoverPageViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, LYFSubscribeTableViewCellDelegate>

@property (nonatomic, retain) UISegmentedControl *segment;/**< navbar上的segment */
@property (nonatomic, retain) UIBarButtonItem *searchButtonItem;/**< 左上角搜索按钮 */
@property (nonatomic, retain) UIBarButtonItem *nearbyButtonItem;/**< 右上角附近按钮 */
@property (nonatomic, retain) UITableView *tableView;/**< 发现主页的tableview */
@property (nonatomic, retain) UIImageView *bigImg;/**< 页面顶部大的图片 */
@property (nonatomic, retain) UILabel *titleLabel;/**< 页面顶部大图片上的文字label */
@property (nonatomic, retain) UIScrollView *scrollView;/**< 底部大的scrollview */
@property (nonatomic, retain) NSMutableArray *dataSource;/**< 数据源 */
@property (nonatomic, retain) UIScrollView *topScrollView;/**< 顶部轮播图 */
@property (nonatomic, retain) UIImageView *placeHolderImg;/**< 订阅页占位图 */
@property (nonatomic, retain) UILabel *placeHolderLabel;/**< 订阅占位图文字 */
@property (nonatomic, retain) UITableView *subscribeTableView;/**< 订阅tableview */
@property (nonatomic, retain) NSMutableArray *arr;/**< 订阅数组 */
@end

@implementation LYFDiscoverPageViewController



- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataSource = [NSMutableArray array];
//        self.arr = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc {
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    // navbar的控件颜色
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.42 green:0.33 blue:0.28 alpha:1];
    // navbar的背景颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:1 green:0.87 blue:0.68 alpha:1];
    
    [self dataHandle];
    [self createSegment];
    [self createBarButtonItem];
    [self createScrollView];
    [self createTableView];
//    [self createPlaceHolderImg];
    

    
    
}

/** 创建顶部segment */
- (void)createSegment {
    self.segment = [[UISegmentedControl alloc] initWithItems:@[@"热吧", @"订阅"]];
    _segment.frame = CGRectMake(SCREEN_WIDTH - (SCREEN_WIDTH / 3 * 2), 5, SCREEN_WIDTH / 3, 30);
    self.navigationItem.titleView = _segment;
    _segment.selectedSegmentIndex = 0;
    [_segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
}

/** 顶部segment点击方法 */
- (void)segmentAction:(UISegmentedControl *) segment {
    self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * _segment.selectedSegmentIndex, 0);
    if (segment.selectedSegmentIndex == 1) {
        
        [[LYFDataBaseHandlle sharedDataBaseHandle] openDB];
        self.arr = [[LYFDataBaseHandlle sharedDataBaseHandle] selectTable];
        [_subscribeTableView reloadData];
        if (_arr.count == 0) {
            [self createPlaceHolderImg];
        } else {

            _placeHolderLabel.alpha = 0;
            _placeHolderImg.alpha = 0;
        }
        
    }
    
}

/** scrollview已经结束滑动,滑动scroll改变seg下标 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
//    CGFloat page =  _scrollView.contentOffset.x / SCREEN_WIDTH;
//    _segment.selectedSegmentIndex = page;
//
    if (_scrollView.contentOffset.x == 0) {
        _segment.selectedSegmentIndex = 0;
        
    } else if (_scrollView.contentOffset.x == SCREEN_WIDTH) {
        _segment.selectedSegmentIndex = 1;
        
        [[LYFDataBaseHandlle sharedDataBaseHandle] openDB];
        self.arr = [[LYFDataBaseHandlle sharedDataBaseHandle] selectTable];
        [_subscribeTableView reloadData];
        
        if (_arr.count == 0) {
            [self createPlaceHolderImg];
        } else {
            [_placeHolderImg removeFromSuperview];
            [_placeHolderLabel removeFromSuperview];
        }
    }
}



/** 创建顶部BarButtonItem */
- (void)createBarButtonItem {
    self.searchButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search.png"] style:0 target:self action:@selector(searchButtonItemAction:)];
    self.navigationItem.leftBarButtonItem = _searchButtonItem;
    
    
    self.nearbyButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fujin.png"] style:0 target:self action:@selector(nearbyButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = _nearbyButtonItem;
     
}

/** 创建底部大的scrollview */
- (void)createScrollView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HETGHT)];
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.bounces = NO;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
}


/** 创建发现主页tableview和头视图 */
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HETGHT - 110) style:UITableViewStylePlain];
    [self.scrollView addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    
    [_tableView registerClass:[LYFdiscoverTableViewCell class] forCellReuseIdentifier:@"LYFDiscoverTableViewCell"];
    
    
    // 创建订阅tableview
    self.subscribeTableView = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HETGHT) style:UITableViewStylePlain];
    _subscribeTableView.delegate = self;
    _subscribeTableView.dataSource = self;
    [_subscribeTableView registerClass:[LYFSubscribeTableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.scrollView addSubview:_subscribeTableView];
    
//    
//    self.bigImg = [[UIImageView alloc] init];
//    _bigImg.frame =  CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HETGHT / 3);
//    _bigImg.backgroundColor = [UIColor redColor];
//    _tableView.tableHeaderView = _bigImg;
//    
//    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, SCREEN_HETGHT / 3 -  (SCREEN_HETGHT / 3 / 8), SCREEN_WIDTH - 10, SCREEN_HETGHT / 3 / 8)];
//    _titleLabel.backgroundColor = [UIColor blueColor];
//    [_bigImg addSubview:_titleLabel];
    
    
    
}

- (void)reloadTableView:(LYFDiscoverModel *)model {
    [_arr removeObject:model];
    [_subscribeTableView reloadData];
    if (_arr.count == 0) {
        [self createPlaceHolderImg];
    }
}

/** 创建顶部轮播图 */
- (void)createTopScrollView {
    LYFDiscoverRotate_bannerModel *dmodel = _model.rotate_bannerModelArr[0];
//    LYFDiscoverRotate_bannerModel *model = _model.rotate_bannerModelArr[1];
//    NSArray *arr = @[model.photoUrl, dmodel.photoUrl];
 NSArray *arr = @[dmodel.photoUrl];
    
    FenglyNuoScrollView *headerView1 = [FenglyNuoScrollView fenglyNuoScrollViewWithImageUrlArray: arr Frame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HETGHT / 4)];
    headerView1.time = 2;
    [headerView1 addSubview:_topScrollView];
    _tableView.tableHeaderView = headerView1;
    
}


/** 创建订阅占位图 */
- (void)createPlaceHolderImg {
    self.placeHolderImg = [[UIImageView alloc] init];
    _placeHolderImg.frame = CGRectMake(SCREEN_WIDTH / 3 + SCREEN_WIDTH, 100, SCREEN_WIDTH / 3, 140);
    _placeHolderImg.image = [UIImage imageNamed:@"subscription-prompt@2x.png"];
    
    
    
    [self.scrollView addSubview:_placeHolderImg];
    
    self.placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 250, SCREEN_WIDTH, 50)];
    _placeHolderLabel.text = @"你还没有订阅，去🔥热吧里逛逛";
    _placeHolderLabel.textAlignment = NSTextAlignmentCenter;
    _placeHolderLabel.textColor = [UIColor grayColor];
    [self.scrollView addSubview:_placeHolderLabel];
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _tableView) {
        
        return _model.category_listModelArr.count;
    } else {
        return _arr.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (tableView == _tableView) {
        
        LYFdiscoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LYFDiscoverTableViewCell" forIndexPath:indexPath];
        LYFDiscoverModel *model = _model.category_listModelArr[indexPath.row];
        cell.model2 = model;
        
//        NSLog(@"%@,", model);
        
        
        
        return cell;
    } else {
        LYFSubscribeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
        
        cell.model = _arr[indexPath.row];
        cell.delegate = self;
        return cell;
    }
    
    return nil;
}

/** tableview点击方法 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LYFDiscoverModel *model = _model.category_listModelArr[indexPath.row];

    LYFDetailsViewController *vc = [[LYFDetailsViewController alloc] init];
    vc.theId = model.theId;
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

/** cell高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _tableView) {
        
        return 100;
    } else {
        return 100;
    }
}

/** 数据解析 */
- (void)dataHandle {
    [WxlAPPTools GETWithURL:@"http://ic.snssdk.com/2/essay/discovery/v3/?iid=3884185944&os_version=9.2.1&os_api=18&app_name=joke_essay&channel=App Store&device_platform=iphone&idfa=F2C266F2-2A1C-4D42-85CC-07B17EE56F90&vid=68B1D0A2-5547-4C3F-B88A-D9B55797F72D&openudid=15d474a633f829aabda5b6e1420e0253418080e8&device_type=iPhone 6 Plus&version_code=5.0.1&ac=WIFI&screen_width=1242&device_id=12502863248&aid=7" par:nil success:^(id responseObject) {
        
        _model = [LYFDiscoverDataModel modelWithDic:responseObject[@"data"]];
        
//        LYFDiscoverRotate_bannerModel *dmodel = _model.rotate_bannerModelArr[0];
//        NSLog(@"%@, %@", dmodel.photoUrl, dmodel.title);
        
        [self createTopScrollView];
        LYFDiscoverModel *dmodel = _model.category_listModelArr[0];
        NSLog(@"%ld, %@", dmodel.total_updates, dmodel.icon_url);
        
        [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];
}

/** 左上角搜索点击方法 */
- (void)searchButtonItemAction:(UIBarButtonItem *) search {
    LYFSearchViewController *vc = [[LYFSearchViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

/** 右上角附近的人点击方法 */
- (void)nearbyButtonItemAction:(UIBarButtonItem *) nearby {
    LYFNearbyViewController *vc = [[LYFNearbyViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end